import React, { Component } from 'react';
import _ from 'lodash';
import Former, { validate } from '../../../components/Former';
import Button from '../../../components/Button';
import { idCardRule } from '../../../utils/rules';

export default class From extends Component {
  _validate = () => {
    validate((err, val) => {
      if (!err && _.isFunction(this.props.onSubmit)) {
        this.props.onSubmit(val);
      }
    });
  };
  render() {
    const fields = [
      {
        label: '姓名',
        placeholder: '请输入真实姓名',
        type: 'TextInput',
        key: 'name',
        initialValue: this.props.user.userInfo.name,
        rules: [{ require: true }]
      },
      {
        label: '身份证号',
        placeholder: '请输入客户身份证号',
        type: 'TextInput',
        key: 'idCardNo',
        initialValue: this.props.user.userInfo.idCardNo,
        rules: [
          {
            validation: (value, cb) => {
              if (!idCardRule(value)) {
                cb('身份证号格式不正确');
              } else {
                cb();
              }
            }
          }
        ]
      },
    ];
    return (
      <Former
        fields={fields}
        bottom={
          <Button
            onPress={this._validate}
            type="primary"
            title="更新个人资料"
          />
        }
      />
    );
  }
}
